package com.elinkway.infinitemovies.view;

import android.app.Activity;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;

import com.elinkway.infinitemovies.listener.MyOnAdListener;
import com.elinkway.infinitemovies.selfdata.MyDataRecordCreater;
import com.elinkway.infinitemovies.selfdata.SelfDataConstant;
import com.elinkway.infinitemovies.selfdata.bean.MyDataRecord;
import com.elinkway.infinitemovies.selfdata.dao.RealmDaoManager;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.elinkway.infinitemovies.utils.UmengEventPoster;
import com.le123.ysdq.R;
import com.qq.e.ads.splash.SplashAD;
import com.qq.e.ads.splash.SplashADListener;

import java.util.HashMap;

/**
 * Date: 2017-05-26
 * Time: 15:13
 * Description: FIXME
 */
public class GdtSplashAdView implements SplashADListener {

    private MyOnAdListener adListener;
    private Activity activity;
    private ViewGroup container;
    private View skipView;
    private SplashAD splashAD;
    private long startRequestTime = 0;
    private String mAdposition;
    private boolean splashIsResponse = false;
    private boolean isOutTimeJump = false;

    public GdtSplashAdView(Activity activity, View skipView, String mAdposition){
        this.activity = activity;
        this.skipView = skipView;
        this.mAdposition = mAdposition;
    }

    public void loadAD(String adId){
        activity.findViewById(R.id.nativeADContainer).setVisibility(View.VISIBLE);
        container = (ViewGroup) activity.findViewById(R.id.baidu_ad_layout);
        container.setVisibility(View.VISIBLE);

        splashAD = new SplashAD(activity, container, skipView, MoviesConstant.GDT_APP_ID, adId, this, 0);
        startRequestTime = System.currentTimeMillis();
        reportAdRequestVtwo();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(!splashIsResponse){
                    if(adListener != null){
                        isOutTimeJump = true;
                        adListener.onAdFailed();
                        selfGdtReport(SelfDataConstant.AC_ADREQUEST_OUTTIME);
                    }
                }
            }
        }, 3000);
    }

    @Override
    public void onADDismissed() {

    }

    @Override
    public void onNoAD(int i) {
        reportAdError(i);
        if(adListener != null){
            if(!isOutTimeJump){
                splashIsResponse = true;
                adListener.onAdFailed();
            }
        }
    }

    @Override
    public void onADPresent() {
        reportAdSuccess();
        if(adListener != null){
            if(!isOutTimeJump){
                splashIsResponse = true;
                adListener.onAdSuccess(null);
                reportAdShow();
            }
        }
    }

    @Override
    public void onADClicked() {
        reportAdClick();
    }

    @Override
    public void onADTick(long l) {

    }

    private void reportAdSuccess(){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        gdtPropertyMap.put("result", "success");
        gdtPropertyMap.put("time", (System.currentTimeMillis()-startRequestTime)/1000+"");
        UmengEventPoster.doPost(UmengEventPoster.GDTAD_REQUEST, gdtPropertyMap);
    }

    private void reportAdRequestError(){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        gdtPropertyMap.put("result", "fail");
        UmengEventPoster.doPost(UmengEventPoster.GDTAD_REQUEST, gdtPropertyMap);
    }

    private void reportAdError(int code){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        gdtPropertyMap.put("code", code+"");
        UmengEventPoster.doPost(UmengEventPoster.GDTAD_ERROE, gdtPropertyMap);

        selfGdtReport(SelfDataConstant.AC_ADREQUEST_FAIL);
    }

    private void reportAdShow() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.GDTAD_SHOW, gdtPropertyMap);

        selfGdtReport(SelfDataConstant.AC_ADSHOW);
    }

    private void reportAdClick() {
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.GDTAD_CLICK, gdtPropertyMap);

        selfGdtReport(SelfDataConstant.AC_ADCLICK);
    }

    private void reportAdRequestVtwo(){
        HashMap<String, String> gdtPropertyMap = new HashMap<>();
        gdtPropertyMap.put("position", mAdposition);
        UmengEventPoster.doPost(UmengEventPoster.GDTAD_REQUEST_VTWO, gdtPropertyMap);

        selfGdtReport(SelfDataConstant.AC_SDKAD_REQUEST);
    }

    public MyOnAdListener getAdListener() {
        return adListener;
    }

    public void setAdListener(MyOnAdListener adListener) {
        this.adListener = adListener;
    }

    private void selfGdtReport(String acode){
        MyDataRecord record = MyDataRecordCreater.build(MyDataRecord.class);
        record.setAcode(acode);
        record.setAd_pro(SelfDataConstant.AD_GDT);
        record.setAd_po(mAdposition);
        RealmDaoManager.addMyDataRecord(record);
    }
}
